<template>
  <div>
    <my-breadcrumb :items="['系统管理', '公式']"/>
    <el-table :data="formulas">
      <el-table-column
        type="index"
        width="55">
      </el-table-column>
      <el-table-column
        label="#"
        prop="remarks">
      </el-table-column>
      <el-table-column
        label="评论数"
        prop="formulaone">
        <template slot-scope="scope">
          评论数 x {{ scope.row.formulaone }}
        </template>
      </el-table-column>
      <el-table-column
        label="点赞数"
        prop="formulatwo">
        <template slot-scope="scope">
          点赞数 x {{ scope.row.formulatwo }}
        </template>
      </el-table-column>
      <el-table-column
        label="收藏量"
        prop="formulathree">
        <template slot-scope="scope">
          收藏量 x {{ scope.row.formulathree }}
        </template>
      </el-table-column>
      <el-table-column
        label="阅读量"
        prop="formulafour">
        <template slot-scope="scope">
          阅读量 x {{ scope.row.formulafour }}
        </template>
      </el-table-column>
      <el-table-column
        label="统计周期"
        prop="week">
      </el-table-column>
      <el-table-column
        label="展示数量"
        prop="count">
      </el-table-column>
      <el-table-column
        label="门槛"
        prop="num">
      </el-table-column>
      <el-table-column
        label="热门展示位置前%"
        prop="hot">
      </el-table-column>
      <el-table-column
        label="操作">
        <template slot-scope="scope">
          <el-button type="text" @click="toUpdate(scope.row)">修改公式</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-dialog title="修改公式" :visible.sync="showAdd">
      <p class="title-formula">{{ type === '3' ? '资讯公式' : '帖子公式' }}</p>
      <p class="title-formula">评论数 x {{ formula.formulaone }} + 点赞数 x {{ formula.formulatwo }} + 收藏量 x {{ formula.formulathree }} + 阅读量 {{ formula.formulafour }}</p>
      <el-form :model="formula" :rules="rules" inline label-width="90px" ref="addForm">
        <el-form-item label="评论数" prop="formulaone">
          <el-input-number v-model="formula.formulaone" :min="0"/>
        </el-form-item>
        <el-form-item label="点赞数" prop="formulatwo">
          <el-input-number v-model="formula.formulatwo" :min="0"/>
        </el-form-item>
        <el-form-item label="收藏量" prop="formulathree">
          <el-input-number v-model="formula.formulathree" :min="0"/>
        </el-form-item>
        <el-form-item label="阅读量" prop="formulafour">
          <el-input-number v-model="formula.formulafour" :min="0"/>
        </el-form-item>
        <el-form-item label="统计周期" prop="week">
          <el-input-number v-model="formula.week" :min="0"/>
        </el-form-item>
        <el-form-item label="展示数量" prop="count">
          <el-input-number v-model="formula.count" :min="0"/>
        </el-form-item>
        <el-form-item label="门槛" prop="num">
          <el-input-number v-model="formula.num" :min="0"/>
        </el-form-item>
        <el-form-item label="热门展示位置前%" prop="hot" label-width="150px">
          <el-input-number v-model="formula.hot" :min="0"/>
        </el-form-item>
      </el-form>
      <div class="footer" slot="footer">
        <el-button type="primary" @click="toSubmit">提交</el-button>
        <el-button type="danger" @click="showAdd = false">取消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
export default {
  name: 'formula',
  data () {
    return {
      formulas: [],
      formula: {
        formulaone: 0,
        formulatwo: 0,
        formulathree: 0,
        formulafour: 0,
        week: 0,
        count: 0,
        num: 0,
        hot: 0
      },
      rules: {
        formulaone: [
          { required: true, message: '不能为空', trigger: 'change' }
        ],
        formulatwo: [
          { required: true, message: '不能为空', trigger: 'change' }
        ],
        formulathree: [
          { required: true, message: '不能为空', trigger: 'change' }
        ],
        formulafour: [
          { required: true, message: '不能为空', trigger: 'change' }
        ],
        week: [
          { required: true, message: '不能为空', trigger: 'change' }
        ],
        count: [
          { required: true, message: '不能为空', trigger: 'change' }
        ],
        num: [
          { required: true, message: '不能为空', trigger: 'change' }
        ],
        hot: [
          { required: true, message: '不能为空', trigger: 'change' }
        ]
      },
      type: '3',
      showAdd: false
    }
  },
  methods: {
    init () {
      this.axios.get('/Config/gettzzxgs')
        .then(res => {
          if (res.data.status === 1) {
            this.formulas = res.data.data
          }
        })
    },

    toUpdate (row) {
      this.type = row.id
      this.showAdd = true
      this.$nextTick(() => {
        Object.keys(this.formula).forEach(key => {
          this.formula[key] = row[key]
        })
        this.$refs['addForm'].clearValidate()
      })
    },

    toSubmit () {
      let params = {}
      Object.keys(this.formula).forEach(key => {
        params[key] = this.formula[key]
      })
      params.id = this.type
      this.$refs['addForm'].validate((valid) => {
        if (valid) {
          this.axios.post('/Config/edittzzxgs', this.qs.stringify(params))
            .then(res => {
              if (res.data.status === 1) {
                this.showAdd = false
                this.init()
                this.$message.success(res.data.msg)
              }
            })
        } else {
          this.$message.error('请检查表单是否填写正确')
        }
      })
    }
  },
  created () {
    this.init()
  }
}
</script>

<style scoped>
  .title-formula{
    padding-left: 20px;
    line-height: 30px;
  }
</style>
